using System;
using System.Collections;
using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class TournamentCareerDialog : MonoBehaviour
{
	public UILabel nameLabel;

	public UISprite flagSprite;

	public UILabel descriptionLabel;

	public UITable table;

	public GameObject stagePrefab;

	public List<UISprite> stars = new List<UISprite>();

	private List<TournamentCareerDialogStage> dialogStages = new List<TournamentCareerDialogStage>();

	private PlayerCareerStageDefinition.StageDefinition stage;

	private Action onComplete;

	public void Show(PlayerCareerStageDefinition.StageDefinition stage, Action onComplete)
	{
		this.onComplete = onComplete;
		NavigationManager.instance.PushModal(base.gameObject);
		this.stage = stage;
		nameLabel.text = stage.name;
		GameConstants.SetFlag(flagSprite, stage.flag);
		PlayerCareer instance = SingletonInit<PlayerCareer>.Instance;
		CareerScoreDAO.StageScore stageScore = stage.stageScore;
		PlayerCareer.StageProgress stageProgress = stage.stageProgress;
		if (stageProgress.isLost)
		{
			stageProgress.Reset();
		}
		int num = 0;
		foreach (PlayerCareerStageDefinition.StageDefinition.SubStageDefinition subStage in stage.subStages)
		{
			CareerProgressDAO.SubStage subStageProgress = stageProgress.GetSubStageProgress(num);
			if (num >= dialogStages.Count)
			{
				GameObject gameObject = NGUITools.AddChild(table.gameObject, stagePrefab);
				gameObject.name = GGFormat.IndexName(num);
				dialogStages.Add(gameObject.GetComponent<TournamentCareerDialogStage>());
			}
			TournamentCareerDialogStage tournamentCareerDialogStage = dialogStages[num];
			tournamentCareerDialogStage.gameObject.SetActive(true);
			tournamentCareerDialogStage.Init(subStage, stageProgress, num);
			num++;
		}
		TournamentCareerDialogStage tournamentCareerDialogStage2 = dialogStages[Mathf.Min(stageProgress.stage.playedGames, dialogStages.Count - 1)];
		table.Reposition();
		CenterOnScroll(tournamentCareerDialogStage2.transform, 5);
		PlayerCareerDialog.SetStarCount(stars, stageScore.stars);
	}

	private void CenterOnScroll(Transform transform, int times)
	{
		StopAllCoroutines();
		StartCoroutine(DoCenterOnScroll(transform, times));
	}

	private IEnumerator DoCenterOnScroll(Transform transform, int times)
	{
		for (int i = 0; i < times; i++)
		{
			UITools.CenterOnScroll(transform, table.transform.parent.GetComponent<UIScrollView>());
			yield return null;
		}
	}

	public void OnPlay()
	{
		if (onComplete != null)
		{
			onComplete();
		}
	}

	public void OnCancel()
	{
		NavigationManager.instance.Pop();
	}
}
